<template>
	<div class="attr">
		<curr-app :left="245"></curr-app>
		<list-title :data="header"></list-title>
		<list-table :list="categoryAttrList" :tableLoading="tableLoading" :footer="true" :pagination="pagination" :appid="$route.query.appid">
			<template v-slot:data>
				<el-table-column prop="attr_name" label="字段名称"></el-table-column>
				<el-table-column prop="attr_enname" label="字段英文名称"></el-table-column>
				<el-table-column prop="attr_type" label="字段类型">
					<template slot-scope="scope">
						<span style="width: 60px">
							{{ (scope.row.attr_type == '1')?'单行文本':'' }}
							{{ (scope.row.attr_type == '2')?'多行文本':'' }}
							{{ (scope.row.attr_type == '3')?'单选框':'' }}
							{{ (scope.row.attr_type == '4')?'复选框':'' }}
							{{ (scope.row.attr_type == '5')?'下拉菜单':'' }}
							{{ (scope.row.attr_type == '6')?'富编辑器':'' }}
							{{ (scope.row.attr_type == '7')?'图片上传':'' }}
							{{ (scope.row.attr_type == '8')?'附件上传':'' }}
							{{ (scope.row.attr_type == '9')?'日期':'' }}
						</span>
					</template>
				</el-table-column>
				<el-table-column label="状态">
					<template slot-scope="scope">
						<el-switch v-model="scope.row.state" @change="changeState(scope.$index, scope.row)"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="100">
					<template slot-scope="scope">
						<el-button type='text' size="mini" @click="handleEdit(scope.$index, scope.row)" v-if="auth.editor">编辑</el-button>
						<el-popover
						placement="top"
						width="160"
						v-model="delTip[scope.$index+1]" v-if="auth.delete">
							<p class="del-tip"><span class="el-icon-question"></span>确定删除吗？</p>
							<div style="text-align: right; margin: 0">
								<el-button size="mini" type="text" @click="delTip.splice(scope.$index+1, 1, false)">取消</el-button>
								<el-button type="primary" size="mini" @click="handleDelete(scope.$index, scope.row)">确定</el-button>
							</div>
							<el-button type="text" slot="reference" :disabled='submitLock[scope.$index+1]'>
								删除 <span class="el-icon-loading" v-if='submitLock[scope.$index+1]'></span>
							</el-button>
						</el-popover>
					</template>
				</el-table-column>
			</template>
		</list-table>
	</div>
</template>

<script>
import ListTitle from "pc/common/Title"
import ListTable from "pc/common/ListTable"
import {changeToBool, judgeAuth} from "static/js/common.js"
import {mapGetters} from 'vuex'
import CurrApp from "pc/common/CurrApp"

export default {
	inject: ['reload'],
	data() {
		return {
			/*appid*/
			appid: this.$route.query.appid,
			/*头部数据*/
			header: {
				title: '字段列表',
				reload: true,
				buttons: [
					{
						title: '添加字段',
						type: 'primary',
						pathName: 'pc/cms/instance/category/CategoryAttr',
						auth: false,
						query: {
							appid: this.$route.query.appid,
						},
					}
				]
			},
			auth: {
				editor: false,//编辑
				delete: false,//删除
				changeState: false,//显示状态
			},
			/*列表数据*/
			categoryAttrList: [
				// {
				// 	id: 1,
				// 	attr_name: '',
				// 	attr_enname: '',
				// 	attr_type: '',
				// 	state: '',
				// },
			],
			pagination: {
				count: 0,
				currPage: 1,
				pageSize: 3
			},
			tableLoading: true,
			delTip: [],//刪除提示
			submitLock: [],
		};
	},
	components: {
		ListTitle,
		ListTable,
		CurrApp
	},
	created: function() {
		/*获取列表数据*/
		this.getCategoryAttrList()
		/*权限判断显示*/
		this.header.buttons[0].auth = (judgeAuth('addCategoryAttr', this.allMenuList) == undefined)?false:true
		this.auth.editor = (judgeAuth('modifyCategoryAttr', this.allMenuList) == undefined)?false:true
		this.auth.delete = (judgeAuth('deleteCategoryAttr', this.allMenuList) == undefined)?false:true
		this.auth.changeState = (judgeAuth('setCategoryAttrState', this.allMenuList) == undefined)?false:true
	},
	watch: {
		/*监听路由变化，根据参数查询数据*/
		'$route' (to){
			this.tableLoading = true
			this.getCategoryAttrList()
		}
	},
	methods: {
		/*取出列表数据*/
		getCategoryAttrList: function(){
			if(this.$api.categoryAttrList != undefined){			
				this.$baseURL(this.$domain.cms)
				this.$get(this.$api.categoryAttrList, this.$route.query).then(res => {
					// console.log(res);
					if(res.data.code == 10007){//后台提示需要登录
						this.$cookie.remove('isLogin');//刪除cookie登录信息
						this.reload();
					}
					if(res.data.code === 0){
						if(res.data.data != undefined){
							this.categoryAttrList = []
							Object.keys(res.data.data.list).forEach((k) => {
								this.categoryAttrList.push ({
									id: res.data.data.list[k].attr_id,
									attr_name: res.data.data.list[k].attr_name,
									attr_enname: res.data.data.list[k].attr_enname,
									attr_type: res.data.data.list[k].attr_type,
									state: changeToBool(res.data.data.list[k].state),
								});
							})
							this.pagination = res.data.data.pageInfo
						}
						this.tableLoading = false
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
		/*编辑*/
    	handleEdit(index, row) {
    		// console.log(row)
			this.$router.push(
				{
					name: 'pc/cms/instance/category/CategoryAttr', 
					query: {
						appid: this.$route.query.appid,
						id: row.id
					}
				}
			)
		},
		/*删除*/
		handleDelete(index, row) {
			if(this.$api.deleteCategoryAttr != undefined){
				if(!this.submitLock[index+1]){
					this.submitLock[index+1] = true//锁住当前的 删除按钮
					this.delTip.splice(index+1, 1, false)//提示消失
					/*请求后台删除*/
					this.$baseURL(this.$domain.cms)
					this.$get(this.$api.deleteCategoryAttr, {
						appid: this.appid,
						'id': row.id
					}).then(res => {
						// if(res.data.code == 10007){//后台提示需要登录
						// 	this.$cookie.remove('isLogin');//刪除cookie登录信息
						// 	this.reload();
						// }
						if(res.data.code == 0){
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'success'
							});
							this.categoryAttrList.splice(index, 1);//删除当前行
						}else{
							this.$message({
								showClose: true,
								message: res.data.info,
								type: 'error'
							});
						}
						this.submitLock[index+1] = false//释放当前的 删除按钮
					})
					.catch(err => {})
					.finally(() => {}) 
				}
			}else{
				this.$message({
					showClose: true,
					message: '您没有更改状态的权限',
					type: 'error'
				});
			}
		},
		/*列表中直接修改 状态*/
		changeState(index, row){
			if(this.$api.setCategoryAttrState != undefined){
				/*请求后台*/
				this.$baseURL(this.$domain.cms)
				this.$post(this.$api.setCategoryAttrState, {
					appid: this.appid,
					'id': row.id,
					'state': row.state
				}).then(res => {
					if(res.data.code == 10007){//后台提示需要登录
						this.$cookie.remove('isLogin');//刪除cookie登录信息
						this.reload();
					}
					if(res.data.code == 0){
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'success'
						});
					}else{
						this.$message({
							showClose: true,
							message: res.data.info,
							type: 'error'
						});
					}
				})
				.catch(err => {})
				.finally(() => {}) 
			}
		},
	},
    computed: {
    	...mapGetters([
			'allMenuList'
		])
    }
};
</script>

<style lang="stylus" scoped>
</style>